﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel.DataAnnotations;

namespace Data
{
    public sealed class DataAll
    {
        private static readonly string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;

        public static DataTable SelectAll(string table, string columns, string parameter)
        {
            return SqlHelper.ExecuteDataset(_connectionString, "ExecuteTable", "select", table, columns, parameter).Tables[0];
        }

        public static string InsertAll(string table, string parameter)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "ExecuteTable", "insertall", table, "", parameter);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string InsertColumns(string table, string columns, string parameter)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "ExecuteTable", "insertcolumns", table, columns, parameter);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string InsertImage(string maanh, string tenanh, DateTime ngaytao, byte[] hinhanh,byte loai, string ghichu)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "sp_DKChung_HinhAnh", 0, maanh, tenanh,
                                           ngaytao, hinhanh,loai, ghichu, "insert");
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string UpdateImage(int maid, string maanh, string tenanh, DateTime ngaytao, byte[] hinhanh,byte loai, string ghichu)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "sp_DKChung_HinhAnh", maid, maanh, tenanh,
                                          ngaytao, hinhanh,loai, ghichu, "update");
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string DeleteImage(int maid)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "sp_DKChung_HinhAnh", maid, "", "", "", "", "", "delete");
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string Update(string table, string parameter)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "ExecuteTable", "update", table, "", parameter);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string Delete(string table, string parameter)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "ExecuteTable", "delete", table, "", parameter);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string InsertCustomer(string parameter)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "ExecuteTable", "customer", "", "", parameter);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static DataTable SelectCustomer(string parameter)
        {
            return SqlHelper.ExecuteDataset(_connectionString, "ExecuteTable", "customer", "", "", parameter).Tables[0];
        }

        public static MemoryStream LoadImage(byte[] image)
        {
            if (image != null)
            {
                using (var ms = new MemoryStream(image, 0, image.Length))
                {
                    ms.Write(image, 0, image.Length);
                    return ms;
                }
            }
            return null;
        }

        public static string DeleteLenh(string IdSanPham)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, "sp_DeleteLenh",IdSanPham);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        public static string GetDislayName<T>(T value)
        {
            var type = typeof(T);
            var member = type.GetMember(value.ToString());
            var attributes = member[0].GetCustomAttributes(typeof(DisplayAttribute), false);
            var name = ((DisplayAttribute)attributes[0]).Name;
            return name;
        }
    }
}
